package com.example.mybatisplus.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.mybatisplus.model.domain.Business;
import com.example.mybatisplus.model.domain.Friends;
import com.example.mybatisplus.mapper.FriendsMapper;
import com.example.mybatisplus.model.domain.Manager;
import com.example.mybatisplus.model.dto.PageDTO;
import com.example.mybatisplus.service.FriendsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lxp
 * @since 2025-03-10
 */
@Service
public class FriendsServiceImpl extends ServiceImpl<FriendsMapper, Friends> implements FriendsService {
    @Autowired
    private FriendsMapper friendsMapper;

    @Override
    public Page<Friends> pageList(Friends friends, PageDTO dto) {

        // 创建查询条件包装器
        QueryWrapper<Friends> wrapper =new QueryWrapper<>();
        // 创建分页对象
        Page<Friends> page = new Page<>(dto.getPageNo(), dto.getPageSize());

        if(Objects.nonNull(friends.getIndirectFriend())){
            wrapper.eq("indirectFriend",friends.getIndirectFriend());
        }

        List<Friends> list =friendsMapper.sortByCount();
        int total = list.size();
        int start = (int) ((page.getCurrent() - 1) * page.getSize());
        int end = Math.min(start + (int)page.getSize(), total);

        List<Friends> pageList = list.subList(start, end);
        page.setTotal(total);
        page.setRecords(pageList);

        // 执行分页查询
//        baseMapper.selectPage(page,wrapper);
        return page;
    }
}
